package se.energimyndigheten.lampguiden.bulbreplacement;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import se.energimyndigheten.lampguiden.HasParallaxView;
import se.energimyndigheten.lampguiden.LightSource;
import se.energimyndigheten.lampguiden.R;

/**
 * Last fragment in "Hitta Lampan", showing the result as a card list
 */
public class ResultCardFragment extends Fragment {

    private static final String TAG = ResultCardFragment.class.getSimpleName();

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    private LightSource[] mLights;
    private ComparisonCalculator mCalculator;
    private HasParallaxView mParallaxParent;

    public static ResultCardFragment newInstance(HasParallaxView parallaxParent, ComparisonCalculator calculator) {
        ResultCardFragment theFragment = new ResultCardFragment();
        theFragment.setCalculator(calculator);
        theFragment.setParallaxParent(parallaxParent);
        return theFragment;
    }

    public void setParallaxParent(HasParallaxView parallaxParent) {
        mParallaxParent = parallaxParent;
    }

    public void setCalculator(ComparisonCalculator calculator) {
        mCalculator = calculator;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        if (mCalculator != null ) {
            mLights = mCalculator.getComparableLightSources();
        }
        return inflater.inflate(R.layout.recycler_view, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.card_list);
        if (mLights != null) {
            setCardLayout();
        }
    }

    @Override
    public void onResume() {
        setCardLayout();
        super.onResume();
    }

    private void setCardLayout() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        // show 1 column in portrait, and two in landscape if more than 1000 pixels width
        int cols = 1;
        if ( getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            cols = displayMetrics.widthPixels < 1001? 1 : 2;
        }

        mLayoutManager = new StaggeredGridLayoutManager(cols, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        /*
        if (mParallaxParent != null) {
            mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    onScrollEffect(dy);
                }

                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (newState == 0 && recyclerView.getChildCount() > 0) {
                        int paddingTop = recyclerView.getPaddingTop();
                        int topItem = recyclerView.getChildAt(0).getTop();
                        if (paddingTop == topItem) {
                            onScrollEffect(-1000);
                        }
                    }
                }
            });
        }
        */
        mAdapter = new ResultCardAdapter(mLights, mCalculator);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // inflate layout again, we might have room for two columns if landscape
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        initView(inflater, (ViewGroup) getView());
    }

    private void initView(LayoutInflater inflater, ViewGroup view) {
        inflater.inflate(R.layout.recycler_view, view);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.card_list);
        setCardLayout();
    }

    public void onDataChanged() {
        mAdapter.notifyDataSetChanged();
    }

    /**
     * Call parent parallax container to update height
     * @param offset how much is scrolled
     */
    private void onScrollEffect(int offset) {
        //Log.v(TAG, "onScrollEffect");
        mParallaxParent.doParallaxScroll(offset);
    }
}
